package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class CookBoy extends BaseRepair {

    public CookBoy(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public void go(){
        String activityIdIn = "9047905,9047906,9047907,9047908";
//        activityIdIn = "9987985";
        Map<String,String> listMap = new ConcurrentHashMap<>();
        List<Map<String,String>> errListMap = new ArrayList<>();
        listMap = getUnionMemberUserId();
        errListMap = getUnionMemberActiivtyErr(listMap,activityIdIn);
        errListMap = getUnionMemberActiivtyErr(errListMap,activityIdIn);
        errListMap = getUnionMemberValueErr(errListMap,activityIdIn);
        System.err.println("------------------------"+errListMap);
        getLogger().error(errListMap);
        DataMgr.addData(errListMap);
    }

    private Map<String,String> getUnionMemberUserId() {
        Map<String,String> dataList = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,unionUid FROM t_u_union_member";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                dataList.put(checkNullToEmpty(rs.getString("userId")),checkNullToEmpty(rs.getString("unionUid")));
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String,String>> getUnionMemberActiivtyErr(Map<String,String> listMap,String activityIdIn) {
        List<Map<String,String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            for(String userId : listMap.keySet()){
                if(StringUtils.isNullOrEmpty(listMap.get(userId))){
                    String sql = "SELECT count(1) as aa FROM t_u_union_activity_condition " +
                            "where memberList like '%"+userId+"%' and activityId in("+activityIdIn+")";
                    call = getGameServer().getConnection().prepareCall(sql);
                    int i=1;
                    rs = call.executeQuery();
                    while (rs.next()) {
                        if(rs.getInt("aa") > 0){
                            Map<String,String> temp = new ConcurrentHashMap<>();
                            temp.put("userId",userId);
                            temp.put("unionUid",listMap.get(userId));
                            dataList.add(temp);
                        }
                        break;
                    }
                }else{
                    String sql = "SELECT count(1) as aa FROM t_u_union_activity_condition " +
                            "where memberList like '%"+userId+"%' and activityId in("+activityIdIn+")";
                    call = getGameServer().getConnection().prepareCall(sql);
                    int i=1;
                    rs = call.executeQuery();
                    while (rs.next()) {
                        if(rs.getInt("aa") > 1){
                            Map<String,String> temp = new ConcurrentHashMap<>();
                            temp.put("userId",userId);
                            temp.put("unionUid",listMap.get(userId));
                            dataList.add(temp);
                        }
                    }
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }


    private List<Map<String,String>> getUnionMemberActiivtyErr(List<Map<String,String>> listMap,String activityIdIn) {
        List<Map<String,String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            for(Map<String,String> map : listMap){
                if(StringUtils.isNullOrEmpty(map.get("unionUid"))){
                    String sql = "SELECT *  FROM t_u_union_activity_condition " +
                            "where memberList like '%"+map.get("userId")+"%' and activityId in("+activityIdIn+") ";
                    call = getGameServer().getConnection().prepareCall(sql);
                    int i=1;
                    rs = call.executeQuery();
                }else{
                    String sql = "SELECT * FROM t_u_union_activity_condition " +
                            "where memberList like '%"+map.get("userId")+"%' and activityId in("+activityIdIn+") and unionUid != '"+map.get("userId")+"'";
                    call = getGameServer().getConnection().prepareCall(sql);
                    int i=1;
                    rs = call.executeQuery();
                }
                while (rs.next()) {
                    Map<String,String> temp = new ConcurrentHashMap<>();
                    temp.put("unionUid",checkNullToEmpty(rs.getString("unionUid")));
                    temp.put("value",checkNullToEmpty(rs.getString("value")));
                    temp.put("userId",map.get("userId"));
                    dataList.add(temp);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String,String>> getUnionMemberValueErr(List<Map<String,String>> listMap,String activityIdIn) {
        List<Map<String,String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            for(Map<String,String> map : listMap){
                String sql = "SELECT *  FROM t_u_activity_relation_condition " +
                        "where userId like '%"+map.get("userId")+"%' and activityId in("+activityIdIn+") and type = 1085";
                call = getGameServer().getConnection().prepareCall(sql);
                int i=1;
                rs = call.executeQuery();
                while (rs.next()) {
                    map.put("userValue",checkNullToEmpty(rs.getString("value")));
                    dataList.add(map);
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
